package com.lvy.hczn.front.business.controller;

import com.lvy.hczn.front.common.annotation.Log;
import com.lvy.hczn.front.common.core.controller.BaseController;
import com.lvy.hczn.front.common.core.domain.AjaxResult;
import com.lvy.hczn.front.common.core.page.TableDataInfo;
import com.lvy.hczn.front.common.enums.BusinessType;
import com.lvy.hczn.front.common.utils.poi.ExcelUtil;
import com.lvy.hczn.front.system.domain.business.Spu;
import com.lvy.hczn.front.system.service.SpuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: 商品管理
 * @projectName: wms-front
 * @package: com.lvy.hczn.front.business.controller
 * @className: SpuAdminController
 * @author: yw
 * @date: 2024/8/16 10:00
 */
@Controller
@RequestMapping("/system/spu/ad")
public class SpuAdminController extends BaseController {

    private String prefix = "system/spu";

    @Autowired
    private SpuService spuService;

    @RequiresPermissions("system:spu:view")
    @GetMapping()
    public String spu() {
        return prefix + "/spu";
    }

    /**
     * 查询原材料列表
     */
    @RequiresPermissions("system:spu:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Spu spu) {
        startPage();
        List<Spu> list = spuService.selectSpuList(spu);
        return getDataTable(list);
    }

    /**
     * 导出原材料列表
     */
    @RequiresPermissions("system:spu:export")
    @Log(title = "原材料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Spu spu) {
        List<Spu> list = spuService.selectSpuList(spu);
        ExcelUtil<Spu> util = new ExcelUtil<Spu>(Spu.class);
        return util.exportExcel(list, "原材料数据");
    }

    /**
     * 新增原材料
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存原材料
     */
    @RequiresPermissions("system:spu:add")
    @Log(title = "原材料", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Spu spu) {
        return toAjax(spuService.insertSpu(spu));
    }

    /**
     * 修改原材料
     */
    @RequiresPermissions("system:spu:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        Spu spu = spuService.selectSpuById(id);
        mmap.put("spu", spu);
        return prefix + "/edit";
    }

    /**
     * 修改保存原材料
     */
    @RequiresPermissions("system:spu:edit")
    @Log(title = "原材料", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Spu spu) {
        return toAjax(spuService.updateSpu(spu));
    }

    /**
     * 删除原材料
     */
    @RequiresPermissions("system:spu:remove")
    @Log(title = "原材料", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(spuService.deleteSpuByIds(ids));
    }
}
